
package com.yesep.learn.mqtt.client.handler.protocol;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.yesep.learn.mqtt.client.common.event.EventType;
import com.yesep.learn.mqtt.client.common.util.ChannelUtil;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.mqtt.MqttConnAckMessage;
import io.netty.handler.codec.mqtt.MqttConnectReturnCode;

/**
   * 消息编号：2
 * @Description: 请求应答
 */
public class ConnAck {

	private static final Logger log = LoggerFactory.getLogger(ConnAck.class);

	public void process(ChannelHandlerContext ctx, MqttConnAckMessage message) {
		ChannelUtil.triggerUserEvent(ctx, EventType.CONNECT_COMPLETE, message);

		if (message.variableHeader().connectReturnCode().equals(MqttConnectReturnCode.CONNECTION_ACCEPTED)) {
			ChannelUtil.triggerUserEvent(ctx, EventType.CONNECT_SUCCESS, message);
			log.info("{} connect success", ChannelUtil.clientId(ctx.channel()));
		} else {
			ChannelUtil.triggerUserEvent(ctx, EventType.CONNECT_FAILURE, message);
			log.info("{} connect failure", ChannelUtil.clientId(ctx.channel()));
		}
	}

}
